"""
Case Type   : 基础功能
Case Name   : 订阅端备升主后，修改删除订阅
Create At   : 2022/11/09
Owner       : opentestcase026
Description :
    1.分别在两个集群创建两张表
    2.创建发布订阅
    3.订阅端备升主 failover
    4.发布端表中插入数据
    5.查询订阅端数据是否同步
    6.修改订阅名
    7.发布端更新数据
    8.订阅端查询数据是否更新
    9.删除订阅
    10.发布端删除数据
    11.订阅端查询数据是否同步
    12.重建订阅
    13.发布端插入数据
    14.订阅端查询数据是否同步
    15.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.数据同步，段页式表查询报错
    6.成功
    7.成功
    8.更新，数据同步
    9.成功
    10.成功
    11.数据不同步
    12.成功
    13.成功
    14.数据不同步
    15.成功
History     :
    Modified by li-xin12345:研发代码变更，段页式表不支持备机读，修改step5预期；
    step3增加刷新动态配置文件步骤
    Modified by li-xin12345 at 2024/9/3:优化用例
    Modified by sungang14 at 2025/9/23:优化用例,当前备机支持查询段页式表
"""

import os
import time
import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '非1+2环境不执行')
class Pubsubclass(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.com_pub = Common()
        self.com_sub = Common('remote1_PrimaryDbUser')
        self.pri_userdb_pub = Node(node='PrimaryDbUser')
        self.pri_userdb_sub = Node(node='remote1_PrimaryDbUser')
        self.std_userdb_sub = Node(node='remote1_Standby1DbUser')
        self.commsh_pub = CommonSH('PrimaryDbUser')
        self.commsh_sub = CommonSH('remote1_PrimaryDbUser')
        self.commsh_sub_std = CommonSH('remote1_Standby1DbUser')
        self.tb_name = ['t_pubsub_0208_1', 't_pubsub_0208_2']
        self.subname = 'sub_pubsub_0208'
        self.pubname = 'pub_pubsub_0208'
        self.port = str(int(self.pri_userdb_pub.db_port) + 1)
        self.user_param_pub = f'-U {self.pri_userdb_pub.db_user} ' \
                              f'-W {self.pri_userdb_pub.db_password}'
        self.user_param_sub = f'-U {self.pri_userdb_sub.db_user} ' \
                              f'-W {self.pri_userdb_sub.db_password}'
        self.wal_level = self.com_pub.show_param("wal_level")
        text = '-----查询集群状态，配置白名单，发布端配置wal_level=logical-----'
        self.log.info(text)
        self.log.info('-----发布端状态-----')
        pub_status = self.commsh_pub.get_db_cluster_status('detail')
        self.log.info(pub_status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, pub_status,
                      f'执行失败:{text}')
        self.log.info('-----订阅端状态-----')
        sub_status = self.commsh_sub.get_db_cluster_status(
            'detail', env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_status)
        self.assertEqual(sub_status.count('Normal'), 4, f'执行失败:{text}')
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  replication  {self.std_userdb_sub.db_user}  '
            f'{self.std_userdb_sub.db_host}/32  sha256')
        self.log.info(guc_res)
        self.assertTrue(guc_res, f'发布端配置白名单失败:{text}')
        guc_res1 = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  replication  {self.pri_userdb_sub.db_user}  '
            f'{self.pri_userdb_sub.db_host}/32  sha256')
        self.log.info(guc_res1)
        self.assertTrue(guc_res1, f'发布端配置白名单失败:{text}')
        if 'logical' != self.wal_level:
            result = self.commsh_pub.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG, 'wal_level=logical')
            self.assertTrue(result, f'设置wal_level失败:{text}')
            restart_res = self.commsh_pub.restart_db_cluster()
            self.assertTrue(restart_res, f'重启数据库失败:{text}')
        guc_res2 = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host  replication  {self.pri_userdb_pub.db_user}  '
            f'{self.pri_userdb_pub.db_host}/32  sha256',
            macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res2)
        self.assertTrue(guc_res2, f'订阅端配置白名单失败:{text}')

    def test_pub_sub(self):
        text = "-----step1:分别在两个集群创建两张表;expect:成功-----"
        self.log.info(text)
        create_tb = f'''drop table if exists {self.tb_name[0]}, 
        {self.tb_name[1]} cascade;
        create table {self.tb_name[0]}(
        id1 int primary key, id2 int, id3 int) with (segment=on);
        create table {self.tb_name[1]}(
        id int primary key constraint id_nn not null,
        use_filename varchar2(20), filename varchar2(255),
        text varchar2(2000)) partition by range(id)(
        partition p1 values less than(30),
        partition p2 values less than(60),
        partition p3 values less than(90),
        partition p4 values less than(maxvalue));'''
        pub_res = self.commsh_pub.execut_db_sql(create_tb,
                                                sql_type=self.user_param_pub)
        self.log.info(pub_res)
        self.assertEqual(pub_res.count(self.constant.TABLE_CREATE_SUCCESS),
                         4, f'执行失败:{text}')
        sub_res = self.commsh_sub.execut_db_sql(
            create_tb, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertEqual(sub_res.count(self.constant.TABLE_CREATE_SUCCESS),
                         4, f'执行失败:{text}')

        text = "-----step2:创建发布订阅;expect:成功-----"
        self.log.info(text)
        create_pub = f"drop publication if exists {self.pubname};" \
                     f"create publication {self.pubname} for all tables;"
        pub_res = self.commsh_pub.execut_db_sql(create_pub,
                                                sql_type=self.user_param_pub)
        self.log.info(pub_res)
        self.assertIn(self.constant.create_pub_succ_msg, pub_res,
                      f'执行失败:{text}')
        guc_res = self.commsh_sub.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.create_keycipher_success, guc_res,
                      f'订阅端主机生成subscription.key.cipher失败:{text}')
        guc_res = self.commsh_sub_std.execute_generate(
            macro.COMMON_PASSWD, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.assertIn(self.constant.create_keycipher_success, guc_res,
                      f'订阅端备机生成subscription.key.cipher失败:{text}')
        create_sub = f"drop subscription if exists {self.subname};" \
                     f"create subscription {self.subname} connection " \
                     f"'host={self.pri_userdb_pub.db_host} " \
                     f"port={self.port} user={self.pri_userdb_pub.db_user} " \
                     f"dbname={self.pri_userdb_pub.db_name} " \
                     f"password={self.pri_userdb_pub.ssh_password}' " \
                     f"publication {self.pubname} with (binary=True);"
        sub_res = self.commsh_sub.execut_db_sql(
            create_sub, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn(self.constant.create_sub_succ_msg, sub_res,
                      f'执行失败:{text}')

        text = "-----step3:订阅端备升主 failover;expect:成功-----"
        self.log.info(text)
        stop_primary = self.commsh_sub.stop_db_instance(
            env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.log.info(stop_primary)
        self.assertIn(self.constant.GS_CTL_STOP_SUCCESS_MSG, stop_primary,
                      f'stop主机失败:{text}')
        sub_result = self.commsh_sub_std.execute_gsctl(
            'failover', self.constant.FAILOVER_SUCCESS_MSG,
            env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.log.info(sub_result)
        res = self.commsh_sub_std.exec_refresh_conf(macro.DB_ENV_PATH_REMOTE1)
        self.log.info(res)
        start_primary = self.commsh_sub.start_db_instance(
            mode="standby", env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.assertTrue(res, f'刷新动态配置文件失败：{text}')
        self.assertTrue(sub_result, f'执行失败:{text}')
        self.assertIn('server started', start_primary, f'start主机失败:{text}')

        text = "-----step4:发布端表中插入数据;expect:成功-----"
        self.log.info(text)
        sql_cmd = f"insert into {self.tb_name[0]} values(" \
                  f"generate_series(1,1000), 1, 1);" \
                  f"insert into {self.tb_name[1]} values(" \
                  f"generate_series(1,1000), 'first', '%一', '');"
        pub_result = self.commsh_pub.execut_db_sql(sql_cmd,
                                                   sql_type=self.user_param_pub)
        self.log.info(pub_result)
        self.assertEqual(pub_result.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         f'执行失败:{text}')

        text = "-----step5:订阅端查询数据是否同步;expect:数据同步，段页式表查询报错-----"
        self.log.info(text)
        self.commsh_pub.check_sub_consistency(
            self.subname, self.user_param_pub, 1200)
        sub_select = f"select count(*) from {self.tb_name[0]};" \
                     f"select count(*) from {self.tb_name[1]};"
        select_res = self.commsh_sub.execut_db_sql(
            sub_select, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(select_res)
        self.assertEqual(select_res.count('1000'), 2, f'执行失败:{text}')

        text = "-----step6:修改订阅名;expect:成功-----"
        self.log.info(text)
        sub_sql = f"alter subscription {self.subname} " \
                  f"rename to new_{self.subname};"
        sub_res = self.commsh_sub_std.execut_db_sql(
            sub_sql, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn(self.constant.alter_sub_succ_msg, sub_res,
                      f'修改订阅名失败:{text}')

        text = "-----step7:发布端更新数据;expect:成功-----"
        self.log.info(text)
        upd_sql = f"update {self.tb_name[0]} set id2=999;" \
                  f"update {self.tb_name[1]} set filename='new';" \
                  f"select * from from pg_replication_slots;"
        upd_res = self.commsh_pub.execut_db_sql(upd_sql,
                                                self.user_param_sub)
        self.log.info(upd_res)
        self.assertEqual(upd_res.count(self.constant.UPDATE_SUCCESS_MSG), 2,
                         f'执行失败:{text}')

        text = "-----step8:订阅端查询数据是否更新;expect:更新，数据同步-----"
        self.log.info(text)
        self.commsh_pub.check_sub_consistency(
            self.subname, self.user_param_pub, 20)
        select_sql = f"select * from {self.tb_name[0]} limit 5;" \
                     f"select * from {self.tb_name[1]} limit 5;"
        select_res = self.commsh_sub_std.execut_db_sql(
            select_sql, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(select_res)
        self.assertIn(self.constant.alter_sub_succ_msg, sub_res,
                      f'刷新订阅失败:{text}')
        self.assertEqual(select_res.count('999'), 5, f'执行失败:{text}')
        self.assertEqual(select_res.count('new'), 5, f'执行失败:{text}')

        text = "-----step9:删除订阅;expect:成功-----"
        self.log.info(text)
        drop_sub = f"drop subscription if exists new_{self.subname};"
        sub_res = self.commsh_sub_std.execut_db_sql(
            drop_sub, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn(self.constant.drop_sub_succ_msg, sub_res,
                      f'删除订阅失败:{text}')

        text = "-----step10:发布端删除表数据;expect:成功-----"
        self.log.info(text)
        upd_sql = f"delete from {self.tb_name[0]};" \
                  f"delete from {self.tb_name[1]};"
        upd_res = self.commsh_pub.execut_db_sql(upd_sql,
                                                self.user_param_sub)
        self.log.info(upd_res)
        self.assertEqual(upd_res.count(self.constant.DELETE_SUCCESS_MSG), 2,
                         f'执行失败:{text}')

        text = "-----step11:订阅端查询数据是否同步;expect:数据不同步-----"
        self.log.info(text)
        sub_select_res = self.commsh_sub_std.execut_db_sql(
            sub_select, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_select_res)
        self.assertEqual(sub_select_res.count('1000'), 2, f'执行失败:{text}')

        text = "-----step12:重建订阅;expect:成功-----"
        self.log.info(text)
        sub_res = self.commsh_sub_std.execut_db_sql(
            create_sub, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_res)
        self.assertIn(self.constant.create_sub_succ_msg, sub_res,
                      f'执行失败:{text}')

        text = "-----step13:发布端插入数据;expect:成功-----"
        self.log.info(text)
        ins_sql = f"insert into {self.tb_name[0]} values(2, 1, 1);" \
                  f"insert into {self.tb_name[1]} " \
                  f"values(2, 'first', '%一', '');"
        ins_res = self.commsh_pub.execut_db_sql(ins_sql,
                                                self.user_param_sub)
        self.log.info(ins_res)
        self.assertEqual(ins_res.count(self.constant.INSERT_SUCCESS_MSG), 2,
                         f'执行失败:{text}')

        text = "-----step14:订阅端查询数据是否同步;expect:数据不同步-----"
        self.log.info(text)
        sub_select_res = self.commsh_sub_std.execut_db_sql(
            sub_select, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_select_res)
        self.assertEqual(sub_select_res.count('1000'), 2, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step15:清理环境;expect:成功-----"
        self.log.info(text)
        subswi_res = self.commsh_sub.execute_gsctl(
            'switchover', self.constant.SWITCH_SUCCESS_MSG,
            env_path=macro.DB_ENV_PATH_REMOTE1,
            dn_path=macro.DB_INSTANCE_PATH_REMOTE1)
        self.log.info(subswi_res)
        subref_res = self.commsh_sub.exec_refresh_conf(
            env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(subref_res)
        drop_sub = f"drop subscription if exists {self.subname};" \
                   f"drop subscription if exists new_{self.subname};"
        drop_sub_res = self.commsh_sub.execut_db_sql(
            drop_sub, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(drop_sub_res)
        drop_pub = f"drop publication if exists {self.pubname}, " \
                   f"new_{self.pubname} cascade;"
        drop_pub_res = self.commsh_pub.execut_db_sql(
            drop_pub, sql_type=self.user_param_pub)
        self.log.info(drop_pub_res)
        drop_tb = f"drop table if exists {self.tb_name[0]}, " \
                  f"{self.tb_name[1]} cascade;"
        pub_droptb = self.commsh_pub.execut_db_sql(
            drop_tb, sql_type=self.user_param_pub)
        self.log.info(pub_droptb)
        sub_droptb = self.commsh_sub.execut_db_sql(
            drop_tb, self.user_param_sub, env_path=macro.DB_ENV_PATH_REMOTE1)
        self.log.info(sub_droptb)
        guc_res = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  replication  {self.std_userdb_sub.db_user}  '
            f'{self.std_userdb_sub.db_host}/32')
        self.log.info(guc_res)
        guc_res1 = self.commsh_pub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  replication  {self.pri_userdb_sub.db_user}  '
            f'{self.pri_userdb_sub.db_host}/32')
        self.log.info(guc_res1)
        guc_res2 = self.commsh_sub.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, macro.DB_INSTANCE_PATH_REMOTE1,
            f'host  replication  {self.pri_userdb_pub.db_user}  '
            f'{self.pri_userdb_pub.db_host}/32', macro.DB_ENV_PATH_REMOTE1)
        self.log.info(guc_res2)
        res_wal = self.commsh_pub.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'wal_level={self.wal_level}')
        self.log.info(res_wal)
        pub_restartdb = self.commsh_pub.restart_db_cluster()
        self.assertTrue(pub_restartdb, f'发布端重启数据库失败:{text}')
        self.assertTrue(subswi_res, f'执行失败:{text}')
        self.assertTrue(subref_res, f'执行失败:{text}')
        self.assertIn(self.constant.drop_sub_succ_msg, drop_sub_res,
                      f'删除订阅失败:{text}')
        self.assertIn(self.constant.drop_pub_succ_msg, drop_pub_res,
                      f'删除发布失败:{text}')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sub_droptb,
                      f'订阅端删除表失败:{text}')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, pub_droptb,
                      f'发布端删除表失败:{text}')
        self.assertTrue(guc_res and guc_res1, f'发布端删除白名单失败:{text}')
        self.assertTrue(guc_res2, f'订阅端删除白名单失败:{text}')
        self.assertTrue(res_wal, f'发布端恢复wal_level参数失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
